<?php
namespace Admin\Model;
use Think\Model;
/**
 * 幻灯片模型
 */
 class SlideModel extends  Model{
 	
	public $_validate = array();
 	/**
	 * 幻灯片添加方法
	 */
	 public function addSlide(){
		$this->_validate =array(
			array('s_title','require','幻灯片标题不能为空'),
		);
		if(!$this->create()){
			return false;
		}
		$data = $_POST;
		//图片上传
		if($_FILES['s_img']['error'] != 4  ){
			//上传图像
			$upload = new \Think\Upload();// 实例化上传类
			$upload->maxSize   =     3145728 ;// 设置附件上传大小
			$upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
			$upload->savePath  =     'Slide/big/'; // 设置附件上传目录
			$upload->saveName  =    'duosute_'.date('ymdHis');
			$upload->autoSub   = 	true;
			$upload->callback  =    false;
			$upload->subName   =    false;
			$file =  $upload->upload();
			if(!$file) {// 上传错误提示错误信息        
				$this->error='上传错误';
				return false;
		    }else{
	    		$path =  'Uploads/'.$file['s_img']['savepath'].$file['s_img']['savename'];//原图样式  大图样式
				$thumbPath = 'Uploads/'.$file['s_img']['savepath'].'thumb/100X100_'.$file['s_img']['savename']; 	
				$thumbPath= str_replace('big/', '', $thumbPath); //组合路径   
		
				$img =  new \Think\Image();
				$img->open($path);
				$img->thumb(100, 100,\Think\Image::IMAGE_THUMB_FIXED)->save($thumbPath);
				$data['s_img'] = $file['s_img']['savename'];//缩图图
		    }
				M('slide')->add($data);
				return true;
		}	
	 }
	 
	 
	 
	 public function editSlide(){
	 	$sid = I('get.sid');
		$this->_validate =array(
			array('s_title','require','幻灯片标题不能为空'),
		);
		if(!$this->create()){
			return false;
		}
		$data = $_POST;
		//图片上传
		if($_FILES['s_img']['error'] != 4 ){
			//上传图像
			$upload = new \Think\Upload();// 实例化上传类
			$upload->maxSize   =     3145728 ;// 设置附件上传大小
			$upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
			$upload->savePath  =     'Slide/big/'; // 设置附件上传目录
			$upload->saveName  =    'duosute_'.date('ymdHis');
			$upload->autoSub   = 	true;
			$upload->callback  =    false;
			$upload->subName   =    false;
			$file =  $upload->upload();
			if(!$file) {// 上传错误提示错误信息        
				$this->error='上传错误';
				return false;
		    }else{
	    		$path =  'Uploads/'.$file['s_img']['savepath'].$file['s_img']['savename'];//原图样式  大图样式
				$thumbPath = 'Uploads/'.$file['s_img']['savepath'].'thumb/100X100_'.$file['s_img']['savename']; 	
				$thumbPath= str_replace('big/', '', $thumbPath); //组合路径   
				p($path);
				p($thumbPath);
				$img =  new \Think\Image();
				$img->open($path);
				$img->thumb(100, 100,\Think\Image::IMAGE_THUMB_FIXED)->save($thumbPath);
				$data['s_img'] = $file['s_img']['savename'];//缩图图
		    }
				
		}	
			M('slide')->where("s_id='{$sid}'")->save($data);
			return true;
	 }
	 
 }
